class ContentManageController < ApplicationController
  before_filter :auth
  def index
  end 
  
  #音乐管理
   def   new_music
    @music=Music.new
   end
   
   def create_music
         @music= Music.new(params[:music])
         @music.filename="music"+@music.filename
         @music.name=params[:name]
         @music.description=params[:description]
         @music.content_id=params[:content_id]
          if  @music.save()
                redirect_to(:action => "index" , :id=>params[:content_id] ) 
              else
                format.html { render :action => "index" }
          end
   end
  
   def  show_music_detail
    @flash=Flash.find(params[:id])
   end 
   
   def  show_music_dir
          @content = Content.find(params[:id])
           @musics=@content.musics
   end
   # 删除 同时删除文件 有问题
       def destroy_music
        sql="delete from musics where id="+params[:id]
           @music = Music.find(params[:id])
           @content=@music.content
           #@music.destroy
           ActiveRecord::Base.connection.execute(sql)
            respond_to do |format|
              format.html { redirect_to(:action=>:"index"  , :id=>@content.id) }
              format.xml  { head :ok }
        end
    end
   #============================================
  #falsh 上传管理
  def  new_flash
       @flash =Flash.new
  end
  
  def create_flash
         @flash= Flash.new(params[:flash])
         @flash.name=params[:name]
         @flash.description=params[:description]
         @flash.content_id=params[:content_id]
         @flash.filename=params[:content_id]+@flash.filename
          if  @flash.save()
                redirect_to(:action => "index" , :id=>params[:content_id] ) 
              else
                format.html { render :action => "index" }
          end
  end
  
   def  show_flash_detail
    @flash=Flash.find(params[:id])
   end 
   
   def  show_flash_dir
          @content = Content.find(params[:id])
           @flashes=@content.flashes
   end
      
      
  def  destroy_flash
    sql="delete from flashes where id="+params[:id]
      @content=Flash.find(params[:id]).content
       ActiveRecord::Base.connection.execute(sql)
        respond_to do |format|
          format.html { redirect_to(:action=>:"index"  , :id=>@content.id) }
          format.xml  { head :ok }
        end
    end
#==============================================================
  #显示
  def show
      @content = Content.find(params[:id])
      if  @content.is_flash=="yes"
               redirect_to(:action => "show_flash_dir" , :id=> @content.id)
       elsif  @content.is_music=="yes"
             redirect_to(:action => "show_music_dir" , :id=> @content.id)
      elsif  @content.is_article=="yes"
       redirect_to(:action => "show_art" , :id=> @content.id)
      elsif  @content.is_article =="no"
       redirect_to(:action => "show_dir" , :id=> @content.id)
      else
       redirect_to(:action => "index" )
      end
  end

  #显示文章
  def show_art
     @content = Content.find(params[:id])
     @images=@content.images
     @image=@images[0]
    # @channel=@content.channel
  end

  #显示目录
  def show_dir
    @content = Content.find(params[:id])
   # @channel=@content.channel
    @contents=[]
    @all_contents=Content.find(:all)
    for c in @all_contents
       if c.parent_id==@content.id
           @contents << c
       end
    end
  end
  
  #删除
  def destroy
    @content = Content.find(params[:id])
    parent_id=@content.parent_id
    @content.destroy
      respond_to do |format|
        format.html { redirect_to(:action => "index" ,:id=>parent_id) }
        format.xml  { head :ok }
      end
  end
  
  # 编辑
  def edit
      @content = Content.find(params[:content])
  end
  
  
  #修改
  def update
      @content = Content.find(params[:content][:id])
      respond_to do |format|
        if @content.update_attributes(params[:content])
            flash[:notice] = '修改成功.'
            format.html { redirect_to(:action => "index" , :id=>params[:content][:id])}
        else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @content.errors, :status => :unprocessable_entity }
        end
      end
  end
  
 #添加新的发布内容
  def new
    @content=Content.new
      @image=Image.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @content }
    end
  end
 
 #创建
 def create_content
     @content = Content.new(params[:content])
     respond_to do |format|
      if @content.save   
          #拷贝图片到指定目录下
         flash[:notice] = '添加成功.'
         format.html { redirect_to(:action => "index" ) }
         format.xml  { render :xml => @content, :status => :created }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @content.errors, :status => :unprocessable_entity }
      end
    end
 end
 
 #创建子目录
 def create_subdir
      @content = Content.new(params[:content])
      respond_to do |format|
         if @content.save 
            flash[:notice] = '创建成功.'
            format.html { redirect_to(:action => "index" , :id=>params[:content][:parent_id]) }
            format.xml  { render :xml => @content, :status => :created }
         else 
            format.html { render :action => "new" }
            format.xml  { render :xml => @content.errors, :status => :unprocessable_entity }
        end
    end
 end
 
 #创建文章
 def create_subart
      @content = Content.new(params[:content])
      @content.is_article="yes"
    respond_to do |format|
      if @content.save   
        flash[:notice] = '创建成功.'
        format.html { redirect_to(:action => "index" , :id=>params[:content][:parent_id]) }
        format.xml  { render :xml => @content, :status => :created }
      else 
        format.html { render :action => "new" }
        format.xml  { render :xml => @content.errors, :status => :unprocessable_entity }
      end
    end
 end
 
 #图片处理
    def new_img
       @image=Image.new
    end
  
    def create_image
         @image = Image.new(params[:image])
         @image.content_id=params[:c_id]
         @image.filename=params[:c_id]+"_"+@image.filename
         if @image.save
             flash[:notice] = 'Image was successfully created.'
             redirect_to(:action=>"index" ,:id=>params[:c_id])
         else
             redirect_to(:action=>"index" ,:id=>params[:c_id])
         end 
     end
       #标记图片是否使用
     def image_using 
         @image = Image.find(params[:id])
         content_id=@image.content_id
         @image.using="yes"
        if @image.save
             flash[:notice] = 'Image was successfully created.'
             redirect_to(:action=>"index" ,:id=>content_id)
         else
             redirect_to(:action=>"index" ,:id=>content_id)
         end 
     end
     
     def image_stopusing 
         @image = Image.find(params[:id])
         content_id=@image.content_id
         @image.using="no"
        if @image.save
             flash[:notice] = 'Image was successfully created.'
             redirect_to(:action=>"index" ,:id=>content_id)
         else
             redirect_to(:action=>"index" ,:id=>content_id)
         end 
     end
     
    #删除channel
    def destroy_image
      @image = Image.find(params[:id])
      @image.destroy
        respond_to do |format|
          format.html { redirect_to(:action=>"index" ,:id=>params[:c_id]) }
          format.xml  { head :ok }
        end
    end
    
     #删除channel
    def destroy_channel
      @channel = Channel.find(params[:id])
      @content=@channel.content
      @channel.destroy
        respond_to do |format|
          format.html { redirect_to(:action=>:"index"  , :id=>@content.id) }
          format.xml  { head :ok }
        end
    end
    
    #新建频道
    def create_channel
      @channel=Channel.new(params[:channel])
      if @channel.save
             flash[:notice] = 'channel was successfully created.'
             redirect_to(:action=>"index" ,:id=>params[:c_id])
         else
             redirect_to( :action=>"index" ,:id=>params[:c_id])
         end 
    end
end
